﻿using System;
using System.Text;

namespace Extensions
{
    /// <summary>
    /// Holds extension methods for <see cref="string"/> class
    /// </summary>
    public static class String
    {
        /// <summary>
        /// Reverses the specified string.
        /// </summary>
        /// <param name="str">The string, which will be reversed.</param>
        /// <returns>The string reversed.</returns>
        public static string Reverse(this string str)
        {
            char[] chars = str.ToCharArray();
            int length = chars.Length;
            char[] reversed = new char[length];
            for (int i = 1; i <= length; i++)
            {
                reversed[ i - 1 ] = chars[ length - i ];
            }
            return new string(reversed);
        }

        //TODO: use char array as Reverse and make public
        private static string Capitilize(this string str)
        {
            StringBuilder capitilized = new StringBuilder(str.Length);
            bool shouldCapitilize = true;
            for (int i = 0; i < str.Length; i++)
            {
                char ch = str[i];
                if (Char.IsWhiteSpace(ch))
                {
                    shouldCapitilize = true;
                    capitilized.Append(ch);
                    continue;
                }
                if ( shouldCapitilize && !Char.IsUpper( ch ) )
                {
                    ch = Char.ToUpper(ch);
                }
                capitilized.Append(ch);

                shouldCapitilize = false;
            }

            return capitilized.ToString();
        }
    }
}
